package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.zabbix.Zabbix;
import cn.sccl.dirms.monitor.model.Host;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 监控主机
 * Created by liupan on 2017/3/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/host")
public class HostController {


    /**
     * 获取某一个组的主机数据
     * @param groupid 组id
     */
    @RequestMapping(value = "/get/{groupid}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> getHostByGroupId(@PathVariable String groupid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("groupids", groupid.split(","));
        params.put("selectInventory","extend"); // 主机资产记录
//        params.put("selectItems","extend"); // 监控项
        Map<String, Object> result = zabbix.call("host.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        for (Map<String, Object> item : mapList) {
            item.put("id", item.get("hostid"));
            item.put("state", "closed");
            item.put("children", new ArrayList<>()); // 监控项
            item.put("type", 2); // 主机类型属性
        }
        return mapList;
    }


    /**
     * 创建主机
     */
    @RequestMapping(value = "/create", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> createHost(@RequestBody Host host) {
        String value ="";
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();

        params.put("host", host.getName());
        params.put("description", host.getDescription());

        List groups = new ArrayList();
        Map<String,String>  mapgroups = new HashMap<>();
        mapgroups.put("groupid", host.getGroupId());
        groups.add(mapgroups);
        params.put("groups", groups);

        List interfaces = new ArrayList();
        Map<String,String> mapinterface = new HashMap<>();
        mapinterface.put("type", host.getType());
        mapinterface.put("main", host.getMain());
        mapinterface.put("useip", host.getUseip());
        mapinterface.put("ip", host.getIp());
        mapinterface.put("dns", host.getDns());
        mapinterface.put("port", host.getPort());
        interfaces.add(mapinterface);
        params.put("interfaces",interfaces);


        List templates = new ArrayList();
        Map<String,String> templateMap = new HashMap<>();
        templateMap.put("templateid", host.getTemplateId());
        templates.add(templateMap);
        params.put("templates", templates);

        Map<String, Object> result = zabbix.call("host.create", params);
        return  result;
    }

    /**
     * 通过host名称获取主机
     * @param host host名称，如IP地址
     * @return
     */
    @RequestMapping(value = "/getByHost", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> getByHost(@RequestBody Host host) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<>();
        params.put("output", "extend");
        Map<String, Object> filter = new HashMap<>();
        List<String> filterContent = new ArrayList<>();
        filterContent.add(host.getName());
        filter.put("host", filterContent);
        params.put("filter", filter);
        Map<String, Object> result = zabbix.call("host.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        if (mapList.size() == 1) {
            return mapList.get(0);
        }
        return null;
    }


    /**
     * 通过id获取host主机信息
     * @param hostid 主机id
     * @return
     */
    @RequestMapping(value = "/getById/{hostid}", method = {RequestMethod.POST, RequestMethod.GET})
    public Map<String,Object> getHostById(@PathVariable String hostid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("hostids", hostid);
        params.put("selectInventory","extend"); // 主机资产记录
//        params.put("selectItems","extend"); // 监控项
        Map<String, Object> result = zabbix.call("host.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        if (mapList.size() == 1) {
            return mapList.get(0);
        }
        return null;
    }

    /**
     * 批量删除host主机信息
     * @return
     */
    @RequestMapping(value = "/delete/ids", method = {RequestMethod.POST, RequestMethod.GET})
    public List<String> deleteByIds(@RequestParam("ids") String ids){
        Zabbix zabbix = Zabbix.getZabbix();
        if(ids != null && ids.length() > 0) {
            String[] idArr = ids.trim().split(",");
            Map<String, Object> resp = zabbix.call("host.delete", Arrays.asList(idArr));
            Map<String, Object> result = (Map<String, Object>) resp.get("result");
            if(result != null) {
                List<String> res = (List<String>) result.get("hostids");
                return res;
            }
        }
        return null;
    }

}
